package com.example.loginmvvm.musicList

import android.os.Bundle
import androidx.lifecycle.Observer
import com.example.loginmvvm.R
import com.example.loginmvvm.base.BaseActivity
import com.example.loginmvvm.player.domain.Music
import kotlinx.android.synthetic.main.activity_musics.*

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/4/19.
 * PS: Not easy to write code, please indicate.
 */
class MusicsActivity : BaseActivity() {

    lateinit var mForeverObserver: ForeverObserver

    private val musicPresenter by lazy {
        MusicPresenter(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_musics)

        initDataListener()
        initViewListener()
    }

    inner class ForeverObserver : Observer<List<Music>> {
        override fun onChanged(t: List<Music>?) {
            println("forever observer data change -->${t?.size}")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        musicPresenter.liveMusicList.removeObserver(mForeverObserver)
    }

    /**
     * 监听数据变换
     */
    private fun initDataListener() {
        mForeverObserver = ForeverObserver()
        /*musicPresenter.liveMusicList.observe(this, Observer {
            //去更新ui
            println("live data 里的音乐列表数据更新了${it.size}")
        })*/

        musicPresenter.liveMusicList.observeForever(mForeverObserver)

        musicPresenter.musicList.addListener(this) {
            println(Thread.currentThread().name)
            //数据变换
            println("数据--->${it?.size}")
            tv_size.text = "数据--->${it?.size}"
        }
        musicPresenter.loadState.addListener(this) {
            println("加载状态--->$it")
        }
    }

    private fun initViewListener() {
        getMusicsBtn.setOnClickListener {
            musicPresenter.getMusic()
        }
    }
}